package com.chinairi.mall.controller;


import com.chinairi.common.utils.ResponseUtil;
import com.chinairi.mall.controller.viewobject.ProjectPlanReqVO;
import com.chinairi.mall.controller.viewobject.ReqModelVO;
import com.chinairi.mall.model.ProjectPlanReq;
import com.chinairi.mall.model.ProjectPlanReqAddress;
import com.chinairi.mall.model.ProjectPlanReqDetail;
import com.chinairi.mall.service.IProjectPlanReqService;
import com.chinairi.mall.service.modelobject.ProjectPlanReqApproveMO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import com.chinairi.common.controller.BaseController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author gsl
 * @since 2020-09-19
 */
@Api("需求计划")
@RestController
@RequestMapping("/project-plan-req")
public class ProjectPlanReqController extends BaseController {


    @Autowired
    private IProjectPlanReqService iProjectPlanReqService;


    /**
     * 生成需求计划
     * 项目服务中心点选生成建议计划时，跳转到页面，此时页面显示项目内容和模型中的数据，项目服务中心用户增增减减物料，点击推送建议计划后，数据插入需求计划表，pStatus和sopStatus同时置为4，表示需求建议计划已下达；
     *
     * @param projectPlanReqVO 项目需求计划
     * @throws IOException
     */
    @PostMapping("/add")
    public void addPlanReq(@RequestBody ProjectPlanReqVO projectPlanReqVO) throws IOException {
        iProjectPlanReqService.addPlanReq(projectPlanReqVO);
        ResponseUtil.showMessage("success", "生成需求计划成功", "");
    }

    /**
     * 单独新增需求计划
     *
     * @param projectPlanReq
     * @throws IOException
     */
    @PostMapping("/addNewReq")
    public void addNewReq(@RequestBody ProjectPlanReq projectPlanReq) throws IOException {
        String planReqId = iProjectPlanReqService.addNewReq(projectPlanReq);
        ResponseUtil.showMessage("success", "生成需求计划成功", planReqId);
    }

    @PostMapping("/exportExcel")
    public void exportExcel(@RequestBody ReqModelVO reqModelVo, HttpServletResponse response) throws IOException {
        iProjectPlanReqService.exportExcel(response, reqModelVo);
    }

    /**
     * 批量设置到货时间和地点
     *
     * @param projectPlanReqDetailList 物料
     * @throws IOException
     */
    @PutMapping("/updateAddressAndTime")
    public void updateAddressAndTime(@RequestBody List<ProjectPlanReqDetail> projectPlanReqDetailList) throws IOException {
        iProjectPlanReqService.updateAddressAndTime(projectPlanReqDetailList);
        ResponseUtil.showMessage("success", "设置收货地址和时间成功", "");
    }

    /**
     * 更新一条需求计划中的物料
     *
     * @param projectPlanReqDetail
     * @throws IOException
     */
    @PutMapping("/updateDetail")
    public void updateDetail(@RequestBody ProjectPlanReqDetail projectPlanReqDetail) throws IOException {
        iProjectPlanReqService.updateDetail(projectPlanReqDetail);
        ResponseUtil.showMessage("success", "更新成功", "");
    }

    /**
     * 新增一条需求计划中的物料
     *
     * @param projectPlanReqDetail
     * @throws IOException
     */
    @GetMapping("/addMaterial")
    public void addMaterial(ProjectPlanReqDetail projectPlanReqDetail) throws IOException {
        iProjectPlanReqService.addMaterial(projectPlanReqDetail);
        ResponseUtil.showMessage("success", "新建成功", "");
    }

    /**
     * 获取本用户单位下的所有需求计划列表
     * 先查询出本单位下的项目
     *
     * @param
     * @throws IOException
     */
    @GetMapping("/getList")
    public void getList(ProjectPlanReq projectPlanReq) throws IOException {
        List<ProjectPlanReq> projectPlanReqList = iProjectPlanReqService.getList(projectPlanReq);
        ResponseUtil.showMessage("success", "获取需求计划成功", projectPlanReqList);
    }

    /**
     * 根据需求计划id获取计划下的物料
     *
     * @param planReqId
     * @throws IOException
     */
    @GetMapping("/getReqDetail")
    public void getReqDetail(String planReqId) throws IOException {
        ProjectPlanReqVO projectPlanReqVO = iProjectPlanReqService.getReqDetail(planReqId);
        ResponseUtil.showMessage("success", "获取需求计划详情成功", projectPlanReqVO);
    }

    /**
     * 根据需求计划id和物料编码获取所属项目地址以及仓库地址
     *
     * @param planReqId   需求计划id
     * @throws IOException e
     */
    @GetMapping("/getAddress")
    public void getProjectAddressOrStorageAddress(String planReqId) throws IOException {
        ProjectPlanReqAddress address = iProjectPlanReqService.getProjectAddressOrStorageAddress(planReqId);
        ResponseUtil.showMessage("success", "获取项目地址成功", address);
    }

    /**
     * 删除计划清单下的一个物料
     *
     * @param planDetailId
     * @throws IOException
     */
    @DeleteMapping("/deleteByDetailId")
    public void deleteByDetailId(String planDetailId) throws IOException {
        iProjectPlanReqService.deleteByDetailId(planDetailId);
        ResponseUtil.showMessage("success", "删除物料成功", "");
    }

    /**
     * 删除某个需求计划
     * 包括其下的物料
     *
     * @param
     * @throws IOException
     */
    @DeleteMapping("/deleteById")
    public void delete(String planReqId) throws IOException {
        iProjectPlanReqService.deleteById(planReqId);
        ResponseUtil.showMessage("success", "删除需求计划成功", "");
    }

    /**
     * 更新状态
     *
     * @param projectPlanReq
     * @throws IOException
     */
    @PutMapping("/updateStatus")
    public void updateStatus(ProjectPlanReq projectPlanReq) throws IOException {
        iProjectPlanReqService.updateStatus(projectPlanReq);
        ResponseUtil.showMessage("success", "审核完成", "");
    }

    /**
     * 提交某条记录进入审核，该接口将这条记录的状态字段置为3-审核中。
     *
     * @param projectPlanReq
     * @throws IOException
     */
    @PutMapping("/addApprove")
    public void addApprove(ProjectPlanReq projectPlanReq) throws IOException {
        iProjectPlanReqService.addApprove(projectPlanReq);
        ResponseUtil.showMessage("success", "提交审核成功", "");
    }

    /**
     * 获取待审核的数据记录，此时审核状态应为3-审核中，或者除了状态为0的其余所有记录均返回即可。
     *
     * @param projectPlanReqApproveMo
     * @throws IOException
     */
    @GetMapping("/getNeedApprove")
    public void getNeedApprove(ProjectPlanReqApproveMO projectPlanReqApproveMo) throws IOException {
        List<ProjectPlanReqApproveMO> list = iProjectPlanReqService.getNeedApprove(projectPlanReqApproveMo);
        ResponseUtil.showMessage("success", "搜索完成", list);
    }

    /**
     * 审核接口，需要前端回传计划主键编号，审核状态，审核意见。后端自动加入审核人和审核时间
     *
     * @param projectPlanReqApproveMo
     * @throws IOException
     */
    @PutMapping("/updateApprove")
    public void updateApprove(ProjectPlanReqApproveMO projectPlanReqApproveMo) throws IOException {
        iProjectPlanReqService.updateApprove(projectPlanReqApproveMo);
        ResponseUtil.showMessage("success", "审核成功", "");
    }

    @PutMapping("projectPlanReq")
    public Map<String,Object> updateProjectPlanReq(@RequestBody ProjectPlanReqVO projectPlanReqVO){
        return iProjectPlanReqService.updateProjectPlanReq(projectPlanReqVO);
    }
}
